<?php

namespace App\Http\Controllers;

use App\Models\RandAd;

class RandAdController extends Controller
{
    public function getRandAd()
    {
        $location = request()->input('city', '000000');
        $currentDay = date('Y-m-d');
        $currentTime = date('H:i:s');
        //生成地址array
        $locationArray = ['000000'];
        $locationArray[] = $location;
        $locationArray[] = substr($location, 0, 4) . '00';
        $locationArray[] = substr($location, 0, 2) . '0000';
        $locationArray = array_unique($locationArray);
        //查询
        $info = RandAd::whereIn('location', $locationArray)
            ->where([
                ['sdate', '<=', $currentDay],
                ['edate', '>=', $currentDay],
                ['stime', '<=', $currentTime],
                ['etime', '>=', $currentTime],
            ])
            ->select('id', 'name', 'adurl')
            ->inRandomOrder()
            ->first();
        return response()->json(['recode' => 1, 'info' => $info]);

    }
}
